package com.zx.controller;


import com.zx.bean.*;
import com.zx.mvc.ResponseBody;
import com.zx.service.AdminService;
import com.zx.service.EnterpriseService;
import com.zx.service.ExpressService;
import com.zx.util.DateFormatUtil;
import com.zx.util.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class EnterpriseController {

    @ResponseBody("/test/enterprise/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String name=request.getParameter("name");

        double profit= Double.parseDouble(request.getParameter("profit"));
        double mobility=Double.parseDouble(request.getParameter("mobility"));
        double payOff=Double.parseDouble(request.getParameter("payOff"));
//        boolean result= AdminService.register(username,password,address,phonenumber,coding);
        Enterprise enterprise=new Enterprise();
        enterprise.setName(name);
        enterprise.setProfit(profit);
        enterprise.setMobility(mobility);
        enterprise.setPayOff(payOff);
        EnterpriseService service=new EnterpriseService();
        boolean result=service.insert(enterprise);
        Message msg= null;
        if (result){
            msg=new Message(0,"插入");
        }else{
            msg=new Message(-1,"插入失败");
        }
        //将数据转化为json并且返回
        String json= JsonUtil.toJson(msg);
        return json;
    }
    @ResponseBody("/test/enterprise/findall.do")
    public String list(HttpServletRequest request, HttpServletResponse response) {
        //1 获取起始索引值
        //int offset = Integer.parseInt(request.getParameter("offset"));
        int offset =1;
        //2 当前页 多少数量
        //int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        int pageNumber =1;
        //3进行查询
        List<Enterprise> itemList = EnterpriseService.findAll(false, offset, pageNumber);
        //4 将集合封装为bootstrap识别的格式  比如时间  状态
        //创建一个返回给前端bootstrap识别集合
        //List<BootStrapTableExpress> list2 = new ArrayList<>();
     //   for (Enterprise e : list) {
//            String intime = DateFormatUtil.format(e.getIntime());
  //          String outtime = e.getOuttime() == null ? "未出库" : DateFormatUtil.format(e.getOuttime());
   //         String status = e.getStatus() == 0 ? "待取件" : "已取件";
    //        String code = e.getCode() == null ? "已取件" : e.getCode();
       //     BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(), e.getNumber(), e.getUsername(), e.getUserphone(), e.getCompany(), code, intime, outtime, status, e.getSysPhone());
          //  list2.add(e2);
       // }
        //获取快递的总数
      //  List<Map<String, Integer>> console = ExpressService.console();
        //int total = console.get(0).get("data1_size");
        //4 将集合封装为bootstap-table 识别的格式
        //ResultData<BootStrapTableExpress> data = new ResultData<>();
        //data.setRows(list2);
        //data.setTotal(total);
        String json = JsonUtil.toJson(itemList);
        return json;
    }
    @ResponseBody("/test/enterprise/analyse.do")
    public String analyse (HttpServletRequest request,HttpServletResponse response){
        Random random=new Random();
        String name=request.getParameter("name");
        double a=random.nextDouble();//盈利对利息覆盖能力
        double b=random.nextDouble();//流动性还本付能力
        double c=random.nextDouble();//清偿性还本付息能力
        int d= (int) (100*random.nextDouble());
        Enterprise enterprise=new Enterprise();
        enterprise.setName(name);
        enterprise.setMobility(a);
        enterprise.setProfit(b);
        enterprise.setPayOff(c);
        enterprise.setGreenRating(d);
        EnterpriseService enterpriseService=new EnterpriseService();
        boolean result=   enterpriseService.insert(enterprise);
        if(result){
            return JsonUtil.toJson("偿债能力分析成功,银行端可查看分析结果");
        }else{
            return JsonUtil.toJson("偿债能力分析失败");
        }
    }

}
